package com.example.javaairportflightanalysis.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.example.javaairportflightanalysis.comparator.StatusComparator;
import com.example.javaairportflightanalysis.dao.CheckMapper;
import com.example.javaairportflightanalysis.dao.UserMapper;
import com.example.javaairportflightanalysis.domin.check.Check;
import com.example.javaairportflightanalysis.domin.check.DfdlCheckAD;
import com.example.javaairportflightanalysis.domin.check.TransferCheckAD;
import com.example.javaairportflightanalysis.domin.merge.DfdlStatusDto;
import com.example.javaairportflightanalysis.domin.merge.TransferStatusDto;
import com.example.javaairportflightanalysis.exception.MyException;
import com.example.javaairportflightanalysis.exception.MyExceptionEnum;
import com.example.javaairportflightanalysis.service.CheckService;
import com.example.javaairportflightanalysis.utils.GroupAndPageUtil;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class CheckServiceImpl implements CheckService {
    @Resource
    CheckMapper checkMapper;
    @Resource
    UserMapper userMapper;
    @Override
    public PageInfo selectAllDate(Integer pageNum, Integer pageSize) {
        List<Check> checks = checkMapper.selectAllDate();
        // 创建一个通用比较器，提取Check对象的status字段值
        StatusComparator<Check> comparator = new StatusComparator<>(Check::getStatus);
        // 对checks列表使用通用比较器进行排序
        checks.sort(comparator);
        PageInfo pageInfo = GroupAndPageUtil.getPageInfo(checks, pageNum, pageSize);
        return pageInfo;
    }
    /**
     * A2 审核同意与拒绝
     */
    @Override
    public List<Object> selectCompareDateA2(Integer id) {
        ArrayList<Object> compareData = new ArrayList<>();
        Check check = checkMapper.selectById(id);
        if (!ObjectUtils.isEmpty(check)){
            String uuid = check.getUuid();
            DfdlCheckAD dfdlCheckAD = checkMapper.selectNewByUuidA(uuid);
            DfdlStatusDto dfdlStatusDto = checkMapper.selectOldByUuidA(uuid);
            compareData.add(dfdlCheckAD);
            compareData.add(dfdlStatusDto);
        }else {
            throw new MyException(MyExceptionEnum.DATA_NOT_FOUND);
        }
        return compareData;
    }
    @Override
    public void acceptCheckA2(DfdlCheckAD dfdlCheckAD){
        //如果同意则将提交的这个dfdlCheckAD的数据更新到Dfdl_A表中
        dfdlCheckAD.setStatus(0);
        checkMapper.updateDfdlCheckA2(dfdlCheckAD);
        //将check_A表中的status更新为2
        checkMapper.updateStatusA2(2,dfdlCheckAD.getUuid());
        //更新审查的表 包含审核人和审核时间，状态
        Object loginIdObj = StpUtil.getLoginId();
        if (loginIdObj instanceof String) {
            Integer loginId = Integer.parseInt((String) loginIdObj);
            Check check = setAuditorCheck(loginId);
            check.setStatus(2);
            check.setUuid(dfdlCheckAD.getUuid());
            checkMapper.updateCheckA(check);
        }
    }
    @Override
    public void refuserCheckA2(DfdlCheckAD dfdlCheckA){
        //3为驳回
        checkMapper.updateStatusA2(3,dfdlCheckA.getUuid());
        //更新审查的表
        Object loginIdObj = StpUtil.getLoginId();
        if (loginIdObj instanceof String) {
            Integer loginId = Integer.parseInt((String) loginIdObj);
            Check check = setAuditorCheck(loginId);
            check.setStatus(3);
            check.setUuid(dfdlCheckA.getUuid());
            checkMapper.updateCheckA(check);
        }
        //还差更新原表，状态变成0
        checkMapper.updateDfdlStatusA2(0,dfdlCheckA.getUuid());
    }
    /**
     * A3审核同意与驳回
     */
    @Override
    public List<Object> selectCompareDateA3(Integer id) {
        ArrayList<Object> compareData = new ArrayList<>();
        Check check = checkMapper.selectById(id);
        if (!ObjectUtils.isEmpty(check)){
            String uuid = check.getUuid();
            TransferCheckAD transferCheckAD = checkMapper.selectNewTransferByUuidA(uuid);
            TransferStatusDto transferStatusDto = checkMapper.selectOldTransferByUuidA(uuid);
            compareData.add(transferCheckAD);
            compareData.add(transferStatusDto);
        }else {
            throw new MyException(MyExceptionEnum.DATA_NOT_FOUND);
        }
        return compareData;
    }
    @Override
    public void acceptCheckA3(TransferCheckAD transferCheckAD) {
        //如果同意则将提交的这个dfdlCheckAD的数据更新到Dfdl_A表中
        transferCheckAD.setStatus(0);
        checkMapper.updateDfdlCheckA3(transferCheckAD);
        //将check_A表中的status更新为2
        checkMapper.updateStatusA3(2,transferCheckAD.getUuid());
        //更新审查的表 包含审核人和审核时间，状态
        Object loginIdObj = StpUtil.getLoginId();
        if (loginIdObj instanceof String) {
            Integer loginId = Integer.parseInt((String) loginIdObj);
            Check check = setAuditorCheck(loginId);
            check.setStatus(2);
            check.setUuid(transferCheckAD.getUuid());
            checkMapper.updateCheckA(check);
        }
    }
    @Override
    public void refuserCheckA3(TransferCheckAD transferCheckAD) {
        checkMapper.updateStatusA3(3,transferCheckAD.getUuid());
        //更新审查的表
        Object loginIdObj = StpUtil.getLoginId();
        if (loginIdObj instanceof String) {
            Integer loginId = Integer.parseInt((String) loginIdObj);
            Check check = setAuditorCheck(loginId);
            check.setStatus(3);
            check.setUuid(transferCheckAD.getUuid());
            checkMapper.updateCheckA(check);
        }
        //还差更新原表，状态变成0
        checkMapper.updateTransferStatusA3(0,transferCheckAD.getUuid());
    }

    /**
     * B2 审核同意与驳回
     */
    @Override
    public List<Object> selectCompareDateD2(Integer id) {
        ArrayList<Object> compareData = new ArrayList<>();
        Check check = checkMapper.selectById(id);
        if (!ObjectUtils.isEmpty(check)){
            String uuid = check.getUuid();
            DfdlCheckAD dfdlCheckAD = checkMapper.selectNewByUuidD(uuid);
            DfdlStatusDto dfdlStatusDto = checkMapper.selectOldByUuidD(uuid);
            compareData.add(dfdlCheckAD);
            compareData.add(dfdlStatusDto);
        }else {
            throw new MyException(MyExceptionEnum.DATA_NOT_FOUND);
        }
        return compareData;
    }
    @Override
    public void acceptCheckD2(DfdlCheckAD dfdlCheckAD){
        //如果同意则将提交的这个dfdlCheckAD的数据更新到Dfdl_A表中
        dfdlCheckAD.setStatus(0);
        checkMapper.updateDfdlCheckD2(dfdlCheckAD);
        //将check_A表中的status更新为2
        checkMapper.updateStatusD2(2,dfdlCheckAD.getUuid());
        //更新审查的表 包含审核人和审核时间，状态
        Object loginIdObj = StpUtil.getLoginId();
        if (loginIdObj instanceof String) {
            Integer loginId = Integer.parseInt((String) loginIdObj);
            Check check = setAuditorCheck(loginId);
            check.setStatus(2);
            check.setUuid(dfdlCheckAD.getUuid());
            checkMapper.updateCheckA(check);
        }

    }
    @Override
    public void refuserCheckD2(DfdlCheckAD dfdlCheckA){
        //3为驳回
        checkMapper.updateStatusD2(3,dfdlCheckA.getUuid());
        //更新审查的表
        Object loginIdObj = StpUtil.getLoginId();
        if (loginIdObj instanceof String) {
            Integer loginId = Integer.parseInt((String) loginIdObj);
            Check check = setAuditorCheck(loginId);
            check.setStatus(3);
            check.setUuid(dfdlCheckA.getUuid());
            checkMapper.updateCheckA(check);
        }
        //还差更新原表，状态变成0
        checkMapper.updateDfdlStatusD2(0,dfdlCheckA.getUuid());
    }

    /**
     * D3审核同意与驳回
     */
    @Override
    public List<Object> selectCompareDateD3(Integer id) {
        ArrayList<Object> compareData = new ArrayList<>();
        Check check = checkMapper.selectById(id);
        if (!ObjectUtils.isEmpty(check)){
            String uuid = check.getUuid();
            TransferCheckAD transferCheckAD = checkMapper.selectNewTransferByUuidD(uuid);
            TransferStatusDto transferStatusDto = checkMapper.selectOldTransferByUuidD(uuid);
            compareData.add(transferCheckAD);
            compareData.add(transferStatusDto);
        }else {
            throw new MyException(MyExceptionEnum.DATA_NOT_FOUND);
        }
        return compareData;
    }
    @Override
    public void acceptCheckD3(TransferCheckAD transferCheckAD) {
        //如果同意则将提交的这个dfdlCheckAD的数据更新到Dfdl_A表中
        transferCheckAD.setStatus(0);
        checkMapper.updateDfdlCheckD3(transferCheckAD);
        //将check_A表中的status更新为2
        checkMapper.updateStatusD3(2,transferCheckAD.getUuid());
        //更新审查的表 包含审核人和审核时间，状态
        Object loginIdObj = StpUtil.getLoginId();
        if (loginIdObj instanceof String) {
            Integer loginId = Integer.parseInt((String) loginIdObj);
            Check check = setAuditorCheck(loginId);
            check.setStatus(2);
            check.setUuid(transferCheckAD.getUuid());
            checkMapper.updateCheckA(check);
        }
    }
    @Override
    public void refuserCheckD3(TransferCheckAD transferCheckAD) {
        checkMapper.updateStatusD3(3,transferCheckAD.getUuid());
        //更新审查的表
        Object loginIdObj = StpUtil.getLoginId();
        if (loginIdObj instanceof String) {
            Integer loginId = Integer.parseInt((String) loginIdObj);
            Check check = setAuditorCheck(loginId);
            check.setStatus(3);
            check.setUuid(transferCheckAD.getUuid());
            checkMapper.updateCheckA(check);
        }
        //还差更新原表，状态变成0
        checkMapper.updateTransferStatusD3(0,transferCheckAD.getUuid());
    }




    private Check setAuditorCheck(Integer id) {
        // auditor 为当前登录的用户
        String auditorUsername =  userMapper.selectById(id).getUsername();

        Check check = new Check();
        //设置修改人
        check.setAuditor(auditorUsername);
        //获取当前修改的时间
        LocalDateTime currentTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedTime = currentTime.format(formatter);
        check.setCheckTime(formattedTime);
        return check;
    }

}
